package com.link.play.fit.ai.ui.main.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.RecyclerView
import com.link.play.fit.ai.R
import com.link.play.fit.ai.bean.InfoIngredientsBean
import com.link.play.fit.ai.bean.ServingTypeBean
import com.link.play.fit.ai.databinding.ItemServingTypeAdapterBinding

// 食物编辑页面的 食物份量按钮的adapter
class ServingTypeAdapter : RecyclerView.Adapter<ServingTypeAdapter.ViewHolder>() {

    //按钮
    private var data = mutableListOf<ServingTypeBean>()
    private var selectData: ServingTypeBean? = null

    var change: ((ServingTypeBean) -> Unit)? = null

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val item = ItemServingTypeAdapterBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(item)
    }

    override fun onBindViewHolder(
        holder: ViewHolder,
        position: Int
    ) {
        val bean = data.get(position)
        holder.setData(bean)
    }

    override fun getItemCount() = data.size

    fun setData(d: List<ServingTypeBean>, s: ServingTypeBean) {
        data = d as MutableList<ServingTypeBean>
        selectData = s
        notifyDataSetChanged()
    }


    inner class ViewHolder(val item: ItemServingTypeAdapterBinding) :
        RecyclerView.ViewHolder(item.root) {

        fun setData(bean: ServingTypeBean) {
            item.button.text = bean.label
            if (selectData?.id == bean.id) {
                item.button.isSelected = true
                item.button.setTextColor("#FFFFFF".toColorInt())
            } else {
                item.button.isSelected = false
                item.button.setTextColor(item.root.resources.getColor(R.color.color_1d1a23))
            }
            item.root.setOnClickListener {
                selectData = bean
                change?.invoke(bean)
                notifyDataSetChanged()
            }
        }
    }
}